<?php $this->load->view('admin/common/header'); ?>
<?php $this->load->view('admin/common/menu'); ?>

<tr>
	<td height='10px'></td>
</tr>
<tr>
	<td align='center'>
		<table cellspacing="0" cellpadding="0" border="0" width="1020px">
			<tbody>
				<tr>
					<td height="67" width="5%" class="item_menu_left">
						</td>
					<td height="67" width="40%" class="item_menu_middle">Thành viên</td>
					<td height="67" width="55%" class="item_menu_right">
						<div>
							<button id="display" name="display" style="float: left; margin-left:10px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Xem&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</button>
							<button id="add" name="add" style="float: left;margin-left:10px;">Thêm mới</button>
							<button id="edit" name="edit" style="float: left;margin-left:10px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sửa&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</button>
							<button id="del" name="del" style="float: left;margin-left:10px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Xóa&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</button>
						</div>

					</td>
				</tr>
			</tbody>
		</table>
	</td>
</tr>
<tr>
	<td height='10px'></td>
</tr>
<tr>
	<td align='center' style="margin-left: 20px">
		<table id="showGrid"></table>
		<div id="pager1"></div> 


<script>

function enterPressed(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	 if (charCode == 13) //Khong xu ly
	 {
		 return false;
	 }//Enter
}
	
jQuery().ready(function ()
 {
/*---------------------------Start Get province----------------------------*/
	$.ajax
    ({
        type: "POST",
        url: '<?php echo base_url(); ?>admin/province/getProvinceList',
        dataType: 'json',
        async: false,
        data: {q:'all'}
       ,success:function(msg)
       {	 
       } 
    }).done(function( msg ) 
    {
		//Fill Data into combobox:
    	var city_edit = $('#city_edit');
		city_edit.find('option').remove().end();
		jQuery.each(msg.rows, function(index, itemData) 
		{
	       city_edit.append("<option rel='"+itemData.id+"' value='"+itemData.id+"'>"+itemData.name+"</option>");   
		});				
    });
/*---------------------------End Get province----------------------------*/
	
	$("#birthday_edit").mask("99/99/9999");  
	jQuery("#showGrid").jqGrid({
	   	url:'<?php echo base_url(); ?>admin/user/getAll',
	   	viewrecords: true,
		multiselect: true,
		mtype: "POST", 
		postData:{act:'viewAll'},
	   	datatype: "json",
	   	colNames:["Id",
		   		  "<?php echo $this->lang->line('username_list'); ?>", 
		   		  "<?php echo $this->lang->line('fullname_list'); ?>",
		   	      "<?php echo $this->lang->line('email_list'); ?>",
		   	      "<?php echo $this->lang->line('group_list'); ?>",
			   	  "<?php echo $this->lang->line('status_list'); ?>",
			   	  "<?php echo $this->lang->line('regisdate_list'); ?>"],
	   	colModel:[
	   		{name:'id',index:'use_id', width:75},
	   		{name:'userName',index:'use_username', width:90},
	   		{name:'fullName',index:'use_fullName', width:100},
	   		{name:'email',index:'use_email', width:80 },
	   		{name:'addr',index:'use_address', width:80},		
	   		{name:'status',index:'use_status', width:50,align:'center'},		
	   		{name:'note',index:'note', width:150, sortable:false}		
	   	],
	   	rowNum:30,
	    width: 1020,
	    height: 'auto',
	   	rowList:[30,50,100,150,200,300],
	   	pager: jQuery('#pager1'),
	   	sortname: 'use_id',
	    viewrecords: true,
	    sortorder: "desc",
	    caption:"Danh sách các thành viên",
	    gridComplete: function()
		{ 
			var ids = jQuery("#showGrid").jqGrid('getDataIDs'); 
		       for(var i=0;i < ids.length;i++)
		       { 
		        	var cl = ids[i];
		        	var ret = jQuery("#showGrid").jqGrid('getRowData',cl);
		        	var status=ret.status;
		        	if(status=='1')
		        	{
		        		status="<a href='#'><img src='<?php echo base_url(); ?>themes/admin/images/active.png'/></a>";
		        	}else
		        	{
		        		status="<a href='#'><img src='<?php echo base_url(); ?>themes/admin/images/deactive.png'href='#'/></a>";
		        	}
		        	jQuery("#showGrid").jqGrid('setRowData',ids[i],{status:status});
		        } 
		    }
		
	}).navGrid('#pager1',{edit:false,add:false,del:false});

	
/* ----------------Thao tac them su xoa -------------------------*/
	$( "#display" )//Hien thi chi tiet
	.button()
	.click(function() 
	{
		processData(this.id);
	});

	$( "#add" )//Them moi user
	.button()
	.click(function() 
	{
		processData(this.id);
	});
	$( "#edit" )//Sua thong tin user
	.button()
	.click(function() 
	{
		processData(this.id);
	});
	$( "#del" )//Xoa thong tin user
	.button()
	.click(function() 
	{
		processData(this.id);
	});
});//end function ready
/*--------------------Start Fill Data---------------------------*/
function fill_Data()
{
	var id = jQuery("#showGrid").jqGrid('getGridParam','selrow'); 
	if (id) 
	{ 
		var ret = jQuery("#showGrid").jqGrid('getRowData',id);
		$('#id_edit').val(ret.id);
		$('#userName_edit').val(ret.userName);
		$('#email_edit').val(ret.email);
		$('#fullName_edit').val(ret.fullName);
		$('#email_edit').val(ret.email);
		$('#confirmEmail_edit').val(ret.email);
		$.ajax
	    ({
	        type: "POST",
	        url: '<?php echo base_url(); ?>admin/user/getOneMember',
	        dataType: 'json',
	        async: false,
	        data: {act:'viewOne',id:ret.id,member:ret.userName}
	       ,success:function(msg)
	       {	 
	       } 
	    }).done(function( msg ) 
	    {
			//Fill Data into combobox:
			jQuery.each(msg.rows, function(index, itemData) 
			{
				 $('#fullName_edit').val(itemData.fullname);
				 $('#address_edit').val(itemData.addr);
				 $('#password_edit').val('');
				 $('#confirmPassword_edit').val('');
				 $('#birthday_edit').val(itemData.birthday);
				 $('#gender_edit').val(itemData.gender);          
				 $('#city_edit').val(itemData.province);
				 $('#sky_edit').val(itemData.sky);
				 $('#mobil_edit').val(itemData.mobil);
				 $('#yahoo_edit').val(itemData.yahoo);
				 $('#capcha_edit').val(''); 		      
			});	
					
	    });  
		return true;
	} else 
	{ 
		alert("Please select row");
		return false;
	}	
}
/*--------------------End Fill Data---------------------------*/
/*--------------------Start clear Data---------------------------*/
function clear_Data()
{
	var blank="";
	$('#id_edit').val(blank);
	$('#userName_edit').val(blank);
	$('#fullName_edit').val(blank);
	$('#address_edit').val(blank);
	$('#password_edit').val(blank);
	$('#confirmPassword_edit').val(blank);
	$('#email_edit').val(blank);
	$('#confirmEmail_edit').val(blank);
	$('#birthday_edit').val(blank);
	$('#gender_edit').val(blank);          
	$('#city_edit').val(blank);
	$('#sky_edit').val(blank);
	$('#mobil_edit').val(blank);
	$('#yahoo_edit').val(blank);
	$('#capcha_edit').val(blank);
	return true;
}
/*--------------------End clear Data---------------------------*/
/*--------------------Start check Data---------------------------*/
function check_Data()
{
	checkAdsession();
	//Tai khoan
    if($("#userName_edit").val()=='')
    {
    	 $("#userName_edit").focus();
    	 $.showErr(" Xin vui lòng nhập thông tin tài khoản!");
    	 addToBasket('bee','userName_edit');
    	 return false;
    }
    //Check pass:
    if($("#password_edit").val()=='')
    {
    	 $("#password_edit").focus();
    	 $.showErr(" Xin vui lòng nhập mật khẩu!");
    	 addToBasket('bee','password_edit');
    	 return false;
    }
     //check confirm pass
	if($('#confirmPassword_edit').val()!=$("#password_edit").val())
	{
		$("#confirmPassword_edit").focus();
     	$.showErr("Xác nhận mật khẩu không giống mật khẩu!");
     	 addToBasket('bee','confirmPassword_edit');
    	return false;
	}
	 //Check email
	if(!$.checkEmail($("#email_edit").val()))
    {
    	 $("#email_edit").focus();
    	 $.showErr(" Địa chỉ email không hợp lệ!");
    	  addToBasket('bee','email_edit');
    	 return false;
    }
    //Confirm Email
	if($("#email_edit").val()!=$("#confirmEmail_edit").val())
    {
    	 $("#confirmEmail_edit").focus();
    	 $.showErr(" Xác nhận Email không đúng!");
    	  addToBasket('bee','confirmEmail_edit');
    	 return false;
    }
	
	//Check fullName:
    if($("#fullName_edit").val()=='')
    {
    	 $("#fullName_edit").focus();
    	 $.showErr(" Xin vui lòng nhập Họ và tên!");
    	 addToBasket('bee','fullName_edit');
    	 return false;
    }
    //Check birthday:
    if(!isDate($("#birthday_edit").val()))
    {
    	 $("#birthday_edit").focus();
    	// $.showErr(" Xin vui lòng nhập ngày sinh hợp lệ: dd/mm/yyyy!");
    	 addToBasket('bee','birthday_edit');
    	 return false;
    }
    
	return true;
}
/*--------------------End check Data---------------------------*/


	 
/*--------------------Start Process Data---------------------------*/

function processData(control)
{
	checkAdsession();
    var openDlg=false;
	//Open dialog:
	jQuery( "#dlg_edit" ).dialog({
		autoOpen: false,
		height: 430,
		width: 800,
		modal: true,
		close: function(ev, ui) 
		{ 		
			
        },
		buttons: {
			Exit: { text:'Thoát',
				 click:function() {
				 	$( this ).dialog( "close" );	
				}	
			},
			Add:{text:'Thêm mới',
				click:function() {
				 
				   if(!check_Data()) return;
				   var method={act:'add'};
				   var myJSONtext=$('#myForm').toJSON();
				   var output = {};
					output = jsonConcat(output, myJSONtext);
					output = jsonConcat(output, method);				
					$.ajax
				    ({
				        type: "POST",
				        url: '<?php echo base_url(); ?>admin/user/process',
				        dataType: 'json',
				        async: true,
				        //json object to sent to the authentication url
				        data:output//JSON.stringify(myJSONtext)
				        ,success:function(msg)
				       {	 
				        } 
				    }).done(function( msg ) 
				    {
				    	if(msg.ret=='success')
						{
				    	   alert('Thanh cong');
				    	   jQuery( "#dlg_edit" ).dialog("close" );
				    	   jQuery("#showGrid").clearGridData();
						   jQuery("#showGrid").trigger("reloadGrid");
						}
						else
						{
							alert('Khong thanh cong!');
						}		
				   });
				}	
		    },
		    Edit:{text:'Sửa',
				click:function() {
				 
				   if(!check_Data()) return;
					var myJSONtext=$('#myForm').toJSON();
					var method={act:'edit'};
					var output = {};
					output = jsonConcat(output, myJSONtext);
					output = jsonConcat(output, method);				
					$.ajax
				    ({
				        type: "POST",
				        url: '<?php echo base_url(); ?>admin/user/process',
				        dataType: 'json',
				        async: true,
				        //json object to sent to the authentication url
				        data:output//JSON.stringify(myJSONtext)
				        ,success:function(msg)
				       {	 
				        } 
				    }).done(function( msg ) 
				    {
				    	if(msg.ret=='success')
						{
				    	  alert('Thanh cong');
				    	  jQuery( "#dlg_edit" ).dialog("close" );
				    	  jQuery("#showGrid").clearGridData();
						  jQuery("#showGrid").trigger("reloadGrid");
						}
						else
						{
							alert('Khong thanh cong!');
						}	
				   });
				}	
		    }
		  }
	});//End open dialog
    
	switch (control)
	{
		case 'display':
			openDlg=fill_Data();
		    break;
		case 'add':
			openDlg=clear_Data();
		    break;
		case 'edit':
			openDlg=fill_Data();
		    break;
		case 'del':
			
			deleteRecord();
			
			break;
		default:
	    alert('No process');
	}
	if(openDlg==true)
	{
		jQuery( "#dlg_edit" ).dialog({autoOpen: true});//End open	
    }
}
/*--------------------End Process Data---------------------------*/

function deleteRecord()
{
	checkAdsession();
	var id = jQuery("#showGrid").jqGrid('getGridParam','selrow'); 
	if (id) 
	{ 
		var ret = jQuery("#showGrid").jqGrid('getRowData',id);
		$( "#dlg_confirm_delete" ).dialog({
			resizable: false,
			height:140,
			
			modal: true,
			buttons: {
			"Xóa": function() {
			        blockflag = true;
			        var id_edit=ret.id;
					var userName_edit=ret.userName;
			        
					var data={act:'del',id_edit:id_edit,userName_edit:userName_edit};
						$.ajax({
						   type: "POST",
						   url:  '<?php echo base_url(); ?>admin/user/process',
						   data: data,
						   dataType: 'json',
						   async:true,
						   error: function(){
		        			 $( "#dlg_error" ).dialog({
					   			modal: true,
					   			buttons: {
					   				OK: function() {
										$( this ).dialog( "close" );
									}
								}
							});
		        			 blockflag = false;
						   },
						   success: function(msg){
						   	if (msg.ret="success"){
						   		alert("Xoa thanh cong!");
						   		jQuery("#showGrid").clearGridData();
								jQuery("#showGrid").trigger("reloadGrid");
						   	}else
						   	{
						   		alert("Xoa khong thanh cong!");
							}
						   }
					    });
				$( this ).dialog( "close" );
			},
			"Thoát": function() {
				$( this ).dialog( "close" );
			}
		}
	});
   }else
   {
	   alert("Please select row!");
   }
}

function authenticate(userName, password) {
	var jsonObjects = [{id:1, name:"amit"}, {id:2, name:"ankit"},{id:3, name:"atin"},{id:1, name:"puneet"}];

    $.ajax
    ({
        type: "POST",
        //the url where you want to sent the userName and password to
        url: '<?php echo base_url(); ?>user/add',
        dataType: 'json',
        async: false,
        //json object to sent to the authentication url
        data: jQuery.parseJSON('{"userName":"'+ userName+'","password":"'+password +'"}'),
        success: function (xml) {
        	alert(xml); 
        }
    });
}

</script>
		<div id="dlg_edit" style="display: none; padding: 5px;"
			title='Sua form'>
			<div class="form-container-dialog ui-helper-clearfix  ui-corner-all">
				<form id='myForm'>
					<table width='730px'>
						<tr>
							<td align="left" valign="top" width="50%">
								<div class="form-panel-auto-dialog">
									<fieldset class="ui-corner-all">
										<table width="100%" height="250px">
											<tr>
												<td align="left" valign="top" width="150px" ><label
													for="id_edit" style='width: 150px;'>Id:</label></td>
												<td align="left" valign="top" width="350px" ><input
													type="hidden" name="id" id="id"
													class="text ui-widget-content ui-corner-all" maxlength="10"
													size="0" /> <input type="text" name="id_edit"
													onkeydown="javascript:return enterPressed(event);"
													id="id_edit" readonly
													class="text ui-widget-content ui-corner-all" maxlength="20"
													size="10"
													style="border: 1px solid red !important; background: #FFF37B !important;" />
												</td>
											</tr>
											<tr>
												<td align="left" valign="top"><label for="userName_edit"
													style="float: left;">Tài khoản:</label></td>
												<td align="left" valign="top"><input type="text"
													name="userName_edit"
													style="float: left; "
													
													id="userName_edit" 
													class="text ui-widget-content ui-corner-all" maxlength="50"
													size="20" /> <font color="#FF0000" style='float: left;'>*</font>

												</td>
											</tr>
											<tr>
												<td align="left" valign="top"><label for="password_edit">Mật
														khẩu:</label></td>
												<td align="left" valign="top"><input type="password"
													name="password_edit" style=''
													id="password_edit" 
													class="text ui-widget-content ui-corner-all" maxlength="30"
													size="25"  /></td>
											</tr>
											<tr>
												<td align="left" valign="top" width="120px"><label
													style="float: left; width: 120px"
													for="confirmPassword_edit">Xác nhận mật khẩu:</label></td>
												<td><input
													
													style="float: left; " type="password"
													name="confirmPassword_edit" id="confirmPassword_edit"
													class="text ui-widget-content ui-corner-all"
													maxlength="30" size="20" /> <span class='yeucau'>*</span>
												</td>
											</tr>
											<tr>
												<td align="left" valign="top" width="120px"><label
													style="float: left; width: 120px" for="email_edit">Email:</label></td>
												<td><input
													
													style="float: left; "
													type="text"
													name="email_edit" id="email_edit"
													class="text ui-widget-content ui-corner-all"
													maxlength="200" size="20" /> <span
													style="color: red; float: left; width: auto;">*</span></td>
											</tr>
											<tr>
												<td align="left" valign="top"><label for="confirmEmail_edit">Xác nhận Email:
														</label></td>
												<td align="left" valign="top"><input
													style="float: left; "
													type="text"
													name="confirmEmail_edit" id="confirmEmail_edit"
													class="text ui-widget-content ui-corner-all"
													maxlength="200" size="20" /> <span class='yeucau'>*</span>
												</td>
											</tr>
											<tr>
												<td align="left" valign="top"><label for="fullName_edit">Họ
														tên:</label></td>
												<td align="left" valign="top"><input
													style="float: left; "
													type="text"
													name="fullName_edit" id="fullName_edit"
													class="text ui-widget-content ui-corner-all"
													maxlength="200" size="20" /> <span class='yeucau'>*</span>
											
											</tr>
											<tr>
												<td align="left" valign="top" width="120px"><label
													style="float: left; width: 120px" for="birthday_edit">Ngày
														sinh:</label></td>
												<td><input
													style="float: left; "
													type="text"
													name="birthday_edit" id="birthday_edit"
													class="text ui-widget-content ui-corner-all"
													maxlength="10" size="10" /> <span class='yeucau'>*</span>
												</td>
											</tr>
										</table>
									</fieldset>
								</div>
							</td>
							<td align="left" valign="top" width="50%">
								<div class="form-panel-auto-dialog">
									<fieldset class="ui-corner-all">
										<table width="100%" height="250px">

											<tr>
												<td align="left" valign="top" width="100px"><label
													style="width: 150px" for="gender_edit">Giới tính:</label></td>
												<td><select name="gender_edit" id="gender_edit"
													style="width: 50px;">
														<option value="0">Nam</option>
														<option value="1">Nữ</option>
												</select> <span class='yeucau'>*</span></td>
											</tr>
											<tr>
												<td align="left" valign="top"><label for="address_edit">Địa
														chỉ:</label></td>
												<td align="left" valign="top"><input type="text"
													name="address_edit" id="address_edit" style="float: left;"
													class="text ui-widget-content ui-corner-all" maxlength="200" size="30"
													onkeydown="javascript:return displayKeyCode(event,this)"
													onkeypress="javascript:return true;" /> <span
													style="color: red; float: left; width: auto;">*</span></td>

											</tr>

											<tr>
												<td align="left" valign="top" width="120px"><label
													style="float: left; width: 120px" for="city_edit">Tỉnh/Thành
														phố:</label></td>
												<td><select type="text" name="city_edit" id="city_edit"
													style="float: left; width:15em;" class="text ui-widget-content ui-corner-all"> 
													</select>
													<span style="color: red; float: left; width: auto;">*</span>
											    </td>
											</tr>

											<tr>
												<td align="left" valign="top" width="120px"><label
													style="float: left; width: 120px" for="tel_edit">Điện
														thoại:</label></td>
												<td><input type="text" style="float: left;" name="tel_edit"
													id="tel_edit" class="text ui-widget-content ui-corner-all"
													maxlength="30" size="25" /> <span
													style="color: red; float: left; width: auto;">*</span></td>
											</tr>
											<tr>
												<td align="left" valign="top" width="120px"><label
													style="float: left; width: 120px" for="mobil_edit">Mobile:</label>
												</td>
												<td><input type="text" style="float: left;"
													name="mobil_edit" id="mobil_edit"
													class="text ui-widget-content ui-corner-all" maxlength="30"
													size="25" /> <span
													style="color: red; float: left; width: auto;">*</span></td>
											</tr>
											<tr>
												<td align="left" valign="top"><label for="sky_edit">Nick
														Sky:</label></td>
												<td align="left" valign="top"><input type="text" size="25px" maxlength="100px"
													style="text-align: right; float: left;" id='sky_edit'
													name='sky_edit'
													class="text ui-widget-content ui-corner-all"> <font
													color="#FF0000" style='float: left;'>*</font></td>
											</tr>
											<tr>
												<td align="left" valign="top"><label for="yahoo_edit">Nick
														Yahoo:</label></td>
												<td align="left" valign="top"><input type="text" size="25px" maxlength="100px"
													style='float: left;' name="yahoo_edit"
													id="yahoo_edit"
													class="text ui-widget-content ui-corner-all"><font
													color="#FF0000" style='float: left;'>*</font></td>
											</tr>

											<tr>
												<td align="left" valign="top"><label for="capcha_edit">Mã
														Xác Thực:</label></td>
												<td align="left" valign="top"><input type="text" size="25px" maxlength="100px"
													id='capcha_edit' name='capcha_edit'
													style="color: red;" class="text ui-widget-content ui-corner-all"></td>
											</tr>

										</table>
									</fieldset>
								</div>
							</td>
						</tr>
					</table>
				</form>
			</div>
		</div>
	</td>
</tr>

<?php $this->load->view('admin/common/footer'); ?>